package com.android.phoenix.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.phoenix.Constant;
import com.android.phoenix.MainApplication;
import com.android.phoenix.R;

public class SettingsActivity extends BaseActivity implements OnClickListener {

	// 超速提醒
	private CheckBox checkSpeedRemind;
	// 休息提醒
	private CheckBox checkRelaxRemind;
	// 安全提醒
	private CheckBox checkSafetyRemind;
	// 修改密码
	private RelativeLayout relativeChangePass;
	// 关于我们
	private RelativeLayout relativeAbout;
	// 软件更新
	private TextView txtUpdate;
	// 推出当前登录
	private Button btnExit;

	/**
	 * 页面返回
	 */
	private ImageView imgBack;

	private SharedPreferences mShare;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acti_settings);
		mShare = MainApplication.mSharedPreferences;

		checkSpeedRemind = (CheckBox) findViewById(R.id.checkSpeedRemind);
		checkRelaxRemind = (CheckBox) findViewById(R.id.checkRelaxRemind);
		checkSafetyRemind = (CheckBox) findViewById(R.id.checkSafetyRemind);
		relativeChangePass = (RelativeLayout) findViewById(R.id.relativeChangePass);
		relativeAbout = (RelativeLayout) findViewById(R.id.relativeAbout);
		txtUpdate = (TextView) findViewById(R.id.txtUpdate);
		btnExit = (Button) findViewById(R.id.btnExit);
		imgBack = (ImageView) findViewById(R.id.imgBack);

		if (mShare.getBoolean(Constant.IS_SAFETY_REMIND_CHECKED, false)) {
			checkSafetyRemind.setChecked(true);
		}

		if (mShare.getBoolean(Constant.IS_SPEED_REMIND_CHECKED, false)) {
			checkSpeedRemind.setChecked(true);
		}

		if (mShare.getBoolean(Constant.IS_RELAX_REMIND_CHECKED, false)) {
			checkRelaxRemind.setChecked(true);
		}

		// 此处三个开关至设定标记值，在要播放地方来根据标记播放
		checkSpeedRemind.setOnCheckedChangeListener(speedRemindListener);
		checkRelaxRemind.setOnCheckedChangeListener(relaxRemindListener);
		checkSafetyRemind.setOnCheckedChangeListener(safetyRemindListener);

		relativeChangePass.setOnClickListener(this);
		relativeAbout.setOnClickListener(this);
		txtUpdate.setOnClickListener(this);
		btnExit.setOnClickListener(this);
		imgBack.setOnClickListener(this);
	}

	private OnCheckedChangeListener speedRemindListener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton button, boolean checked) {
			if (checked) {
				Constant.SPEED_REMIND = true;
				mShare.edit()
						.putBoolean(Constant.IS_SPEED_REMIND_CHECKED, true)
						.commit();
			} else {
				Constant.SPEED_REMIND = false;
				mShare.edit()
						.putBoolean(Constant.IS_SPEED_REMIND_CHECKED, false)
						.commit();
			}

		}
	};

	private OnCheckedChangeListener relaxRemindListener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton button, boolean checked) {
			if (checked) {
				Constant.RELAX_REMIND = true;
				mShare.edit()
						.putBoolean(Constant.IS_RELAX_REMIND_CHECKED, true)
						.commit();
			} else {
				Constant.RELAX_REMIND = false;
				mShare.edit()
						.putBoolean(Constant.IS_RELAX_REMIND_CHECKED, false)
						.commit();
			}

		}
	};

	private OnCheckedChangeListener safetyRemindListener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton button, boolean checked) {
			if (checked) {
				mShare.edit()
						.putBoolean(Constant.IS_SAFETY_REMIND_CHECKED, true)
						.commit();
			} else {
				mShare.edit()
						.putBoolean(Constant.IS_SAFETY_REMIND_CHECKED, false)
						.commit();
			}

		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.relativeChangePass:
			// 修改密码,跳转到修改密码的页面；
			changePass();
			break;
		case R.id.relativeAbout:
			// 关于我们
			break;
		case R.id.txtUpdate:
			// 软件更新
			update();
			break;
		case R.id.btnExit:
			// 退出当前登录
			exitLogin();
			break;
		case R.id.imgBack:
			// 退出当前页面
			exitActivity();
		}
	}

	private void exitActivity() {
		finish();

	}
   /*
	private void palyVoice(String url) {
		try {
			MediaPlayer player = new MediaPlayer();
			player.setDataSource(url);
		} catch (Exception e) {
			e.printStackTrace();
			Toast.makeText(SettingsActivity.this,
					getResources().getString(R.string.paly_failed),
					Toast.LENGTH_SHORT).show();
		}
	}
   */
	private void changePass() {
		Intent intent = new Intent();
		intent.setClass(this, ChangePassActivity.class);
		startActivity(intent);
	}

	private void exitLogin() {
		MainApplication.mSharedPreferences.edit().putBoolean(
				Constant.LOGIN_SUCCEED, false).commit();
		Intent intent = new Intent();
		intent.setClass(this, LoginActivity.class);
		startActivity(intent);
		finish();
	}

	private void update() {
	}

}
